package com.lanou.controller;

import com.lanou.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description: Hello World!
 * author: 何雨轩
 * Create at: 18/7/18 下午7:41
 */
@Controller
public class UserController {

    @RequestMapping("/login")
    public String login(User user, Model model) {
        // 主体(登入的人)
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());

        try {
            subject.login(token);
       // } catch (UnknownAccountException) { // 未知账户异常

       // }catch (IncorrectCredentialsException){ // 密码错误异常

        }catch (Exception e){
            System.out.println("用户名/密码错误");
            model.addAttribute("msg","用户名/密码错误");
            return "/login.jsp";
        }

        return "redirect:success.jsp";
    }
}
